/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kiakaku.downloadpage;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.TextExtractor;
import org.kiakaku.utility.FileUtility;

/*
 *   File download is created by kiakaku on Mar 15, 2012 9:13:52 PM
 * If you want modify this file please insert information in here 
 * - Last modify in Mar 15, 2012, 9:13:52 PM by kiakaku
 *                  
 *
 * @author kiakaku
 */
public class download {

    List<Node> all = null;
    int not = 0;

    public download() {
        all = FileUtility.docListOne("list_all_node.txt");

    }

    public void downloadAll() {
        for (Node node : all) {
            downloadOneSite(node);
        }
    }

    public void downloadOneSite(Node currNode) {
        File a = new File("data\\" + currNode.hashCode() + ".txt");
        boolean exs = a.exists();
        if (exs == true) {
            System.out.println("Check done" + currNode.getId());
            return;
        }
        System.out.println("not done: " + currNode.hashCode());
        System.out.println(currNode.getUrl());
//        try {
//            not++;
//            System.out.println("Check not done (" + not + "): " + currNode.hashCode());
//            Source source = new Source(new URL(currNode.getUrl()));
//            source.setLogger(null);
//            List<Element> div = source.getAllElements(HTMLElementName.DIV);
//            // download site
//            Element chi_tiet = null;
//            for (Element temp : div) {
//                if ("ctl00_IDContent_Tin_Chi_Tiet".equalsIgnoreCase(temp.getAttributeValue("id"))) {
//                    chi_tiet = temp;
//                    break;
//                }
//            }
//            if (chi_tiet == null) {
//                for (Element temp : div) {
//                    if ("container".equalsIgnoreCase(temp.getAttributeValue("class"))) {
//                        chi_tiet = temp;
//                        break;
//                    }
//                }
//            }
//            if (chi_tiet == null) {
//                for (Element temp : div) {
//                    if ("detail".equalsIgnoreCase(temp.getAttributeValue("class"))) {
//                        chi_tiet = temp;
//                        break;
//                    }
//                }
//            }
//            if (chi_tiet == null) {
//                for (Element temp : div) {
//                    if ("homehighlight-left".equalsIgnoreCase(temp.getAttributeValue("class"))) {
//                        chi_tiet = temp;
//                        break;
//                    }
//                }
//            }
//            if (chi_tiet == null) {
//                chi_tiet = div.get(0);
//            }
//            TextExtractor txtExtract = new TextExtractor(chi_tiet) {
//
//                @Override
//                public boolean excludeElement(StartTag st) {
//                    if (st.getName().equalsIgnoreCase(HTMLElementName.DIV) && "list-video-header".equalsIgnoreCase(st.getAttributeValue("class"))) {
//                        return true;
//                    }
//                    return false;
//                }
//            };
//            System.out.println("download: " + currNode.getUrl());
//            FileUtility.ghiFile(txtExtract.toString(), "def\\" + currNode.hashCode() + ".txt");
//        } catch (IOException ex) {
////            Logger.getLogger(OutLink.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    public static void main(String[] args) {
        new download().downloadAll();
    }
}
